package com.xzmzhp.dao.cos.sql;

import com.xzmzhp.utils.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-04-23 20:17
 * @program: ams
 * @description: cos相关sql
 */
public class CosSql {

    /**
     * 查询所有cos服相关信息
     * @param param 参数
     *        cosName cos服名称
     *        applicablePeople 适用人群
     *        belongingTo 所属
     *        currentHolder 持有人
     * @return cos服相关信息
     */
    public String findAll(Map<String, Object> param){
        //cos服名称
        String cosName = param.get("cosName").toString();
        //适用人群
        Integer applicablePeople = Integer.parseInt(param.get("applicablePeople").toString());
        //所属
        Integer belongingTo = Integer.parseInt(param.get("belongingTo").toString());
        //持有人
        String currentHolder = param.get("currentHolder").toString();
        return new SQL(){
            {
                SELECT("cos_id,cos_name,applicable_people,belonging_to,cos_prices,current_holder,cos_status");
                FROM("cos");
                if (StringUtils.isNotEmpty(cosName)) {
                    WHERE("cos_name = '%" + cosName + "%'");
                }
                if (StringUtils.isNotNull(applicablePeople)) {
                    WHERE("applicable_people = " + applicablePeople);
                }
                if (StringUtils.isNotNull(belongingTo)) {
                    WHERE("belonging_to = " + belongingTo);
                }
                if (StringUtils.isNotEmpty(currentHolder)) {
                    WHERE("current_holder = '%" + currentHolder + "%'");
                }
            }
        }.toString();
    }

    /**
     * 根据条件查询cos服相关信息sql
     * @param param 菜蔬
     *        cosName cos服名称
     *        applicablePeople 适用人群
     *        belongingTo 所属
     *        currentHolder 持有人
     *        minPrices 最低价
     *        maxPrices 最高价
     * @return sql
     */
    public String cosByParam(Map<String,Object> param){
        //cos服名称
        String cosName = param.get("cosName").toString();
        //适用人群
        Integer applicablePeople = Integer.parseInt(param.get("applicablePeople").toString());
        //所属
        Integer belongingTo = Integer.parseInt(param.get("belongingTo").toString());
        //持有人
        String currentHolder = param.get("currentHolder").toString();
        //cos服价格
        BigDecimal minPrices =new BigDecimal(param.get("minPrices").toString());
        BigDecimal maxPrices =new BigDecimal(param.get("maxPrices").toString());
        return new SQL(){
            {
                SELECT("cos_id,cos_name,applicable_people,belonging_to,cos_prices,current_holder,cos_status,breakage_type,lend_status");
                FROM("cos,breakage,lend");
                WHERE("cos.cos_id = breakage.item_id,cos.cos_id = lend.item_id");
                if (StringUtils.isNotEmpty(cosName)) {
                    WHERE("cos_name = '%" + cosName + "%'");
                }
                if (StringUtils.isNotNull(applicablePeople)) {
                    WHERE("applicable_people = " + applicablePeople);
                }
                if (StringUtils.isNotNull(belongingTo)) {
                    WHERE("belonging_to = " + belongingTo);
                }
                if (StringUtils.isNotEmpty(currentHolder)) {
                    WHERE("current_holder = '%" + currentHolder + "%'");
                }
                if (StringUtils.isNotNull(minPrices)) {
                    WHERE("cos_prices >= " + minPrices);
                }
                if (StringUtils.isNotNull(maxPrices)) {
                    WHERE("cos_prices <= " + maxPrices);
                }
                ORDER_BY("breakage_type,cos_status,lend_status");
            }
        }.toString();
    }
}
